package com.system.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Classname FileUtils
 * @Description TODO
 * @Date 2023/9/17 13:17
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FileUtils {


    public static List<File> getFiles(String path){
        File file = new File(path);
        if(!file.exists()){
            return Collections.emptyList();
        }
        List<File> fileLists = new ArrayList<>();
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                if(f.isDirectory()){
                    getFiles(fileLists,f.listFiles());
                }else {
                    fileLists.add(f);
                }
            }
        }else {
            fileLists.add(file);
        }
        return fileLists;
    }


    public static String getFileType(File file){
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex != -1 && dotIndex < fileName.length() - 1) {
            String fileType = fileName.substring(dotIndex + 1);
            System.out.println("文件类型为: " + fileType);
            return fileType;
        } else {
            System.out.println("无法获取文件类型");
            return fileName;
        }
    }

    private static void getFiles(List<File> fileList,File[] files){
        for (File f : files) {
            if(f.isDirectory()){
                getFiles(fileList,f.listFiles());
            }else {
                fileList.add(f);
            }
        }
    }
}
